//
//  PerformanceVC.swift
//  kxf
//  业绩
//  Created by Mac on 2024/3/24.
//

import Foundation
import MCToast


class PerformanceVC: WHBaseViewVC {
    
    var startTime = Date().getLast30Days()
    var endTime = Date().todayDate
    
    var agentResultObj = NSDictionary()
    var subResultObj = NSDictionary()
    var totalTeamResultObj = NSDictionary()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        sendDateRequest()
    }
    lazy var topVm : PerformanceFilterVM = {
        let vm = PerformanceFilterVM.init(frame: CGRect.init(x: 0, y: getNavigationBarHeight(), width:0, height: 0))
        vm.timeFilterAlertVm.controller = self
        vm.controller = self
        vm.timeFilterAlertVm.queryBlock = {(startTime,endTime)in
            self.topVm.dataTitleVm.contentLabel.text = "\(startTime)  -  \(endTime)"
            self.startTime = startTime
            self.endTime = endTime
            self.sendDateRequest()
        }
        vm.queryBlock = {(startTime,endTime)in
            self.topVm.dataTitleVm.contentLabel.text = "\(startTime)  -  \(endTime)"
            self.startTime = startTime
            self.endTime = endTime
            self.sendDateRequest()
        }
        return vm
    }()
    lazy var teamPerformanceVm : PerformanceTeamMsgVM = {
        let vm = PerformanceTeamMsgVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        
        return vm
    }()
    lazy var myMercPerformanceVm : PerformanceMyMercVM = {
        let vm = PerformanceMyMercVM.init(frame: CGRect.init(x: 0, y: self.teamPerformanceVm.frame.maxY + kFitWidth(10), width: 0, height: 0))
        vm.tapBlock = {()in
            let vc = MercPermanceListVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
    lazy var serviceMercVm : PerformanceServiceMercVM = {
        let vm = PerformanceServiceMercVM.init(frame: CGRect.init(x: 0, y: self.myMercPerformanceVm.frame.maxY+kFitWidth(10), width: 0, height: 0))
        vm.tapBlock = {()in
            let vc = AgentPermanceListVC()
            self.navigationController?.pushViewController(vc, animated: true)
        }
        return vm
    }()
}

extension PerformanceVC{
    func initUI(){
        initNavi(titleStr: "业绩")
        
        view.backgroundColor = .COLOR_LIGHT_GREY
        view.addSubview(topVm)
        
        view.addSubview(scrollView)
        scrollView.frame = CGRect.init(x: 0, y: self.topVm.frame.maxY, width: SCREEN_WIDHT, height: SCREEN_HEIGHT-self.topVm.frame.maxY)
        
        scrollView.addSubview(teamPerformanceVm)
        scrollView.addSubview(myMercPerformanceVm)
        scrollView.addSubview(serviceMercVm)
        
        scrollView.contentSize = CGSize.init(width: 0, height: self.serviceMercVm.frame.maxY+kFitWidth(10))
    }
}

extension PerformanceVC{
    func sendDateRequest() {
        MCToast.mc_loading()
        let urlString = "\(URL_AgentStatHistory)?startTime=\(startTime)&endTime=\(endTime)"
        WHNetworkUtil.shareManager().GET(urlString: urlString) { responseObject in
            MCToast.mc_remove()
            DLLog(message: "\(responseObject)")
            
            if responseObject["code"]as? Int ?? 0 == 200{
                let dataObj = responseObject["data"]as? NSDictionary ?? [:]
                self.agentResultObj = dataObj["agentResult"]as? NSDictionary ?? [:]
                self.subResultObj = dataObj["subResult"]as? NSDictionary ?? [:]
                self.totalTeamResultObj = dataObj["totalTeamResult"]as? NSDictionary ?? [:]
                
                self.teamPerformanceVm.updateUI(dict: self.totalTeamResultObj)
                self.myMercPerformanceVm.updateUI(dict: self.agentResultObj)
                self.serviceMercVm.updateUI(dict: self.subResultObj)
            }else{
                self.presentAlertVcNoAction(title: "\(responseObject["message"]as? String ?? "网络异常")", viewController: self)
            }
            
        }
    }
}
